//
//  MainTabBarController.swift
//  SinaWeibo
//
//  Created by paycloud110 on 16/5/30.
//  Copyright © 2016年 paycloud110. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {

    private var jsonObjc: NSArray?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 动态加载控制器；在需要的时候显示对应的控制器文件和内容 用到指定时间使用不同的控制器和功能
        guard let jsonPath = NSBundle.mainBundle().pathForResource("Controller.json", ofType: nil) else
        {
            JWNLog("json路径获取失败")
            return
        }
        guard let jsonData = NSData(contentsOfFile: jsonPath) else
        {
            JWNLog("json转data数据失败")
            return
        }
        do {
            jsonObjc = try NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers) as? NSArray
        }catch {
            JWNLog(error)
        }
        var index: Int?
        for var i = 0; i < (jsonObjc?.count)! + 1; i++ {
            index = i
            if index == 2 {
                addChildViewController(UIViewController(), imageNameNomal: "", title: "")
                continue
            }else {
                if index > 2 {
                    index = i - 1
                }
            }
            let dict: NSDictionary = jsonObjc![index!] as! NSDictionary
            addChildViewController(classObjcFromControllerName(dict["vcName"]! as! String), imageNameNomal: dict["tabBarItemImage"] as! String, title: dict["title"] as! String)
        }
        addComposeButton()
    }
    private func addChildViewController(childController: UIViewController?, imageNameNomal: String, title: String)
    {
        guard let childVC = childController else
        {
            return
        }
        childVC.tabBarItem.image = UIImage(named: imageNameNomal)
        childVC.tabBarItem.selectedImage = UIImage(named: imageNameNomal + "_highlighted")
        childVC.title = title
        let nav = UINavigationController.init(rootViewController: childVC)
        addChildViewController(nav)
        childVC.tabBarController?.tabBar.tintColor = UIColor.orangeColor()
    }
    // 由类名获取对应类
    private func classObjcFromControllerName(childControllerName: String) -> UIViewController?
    {
        guard let spaceName = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as? String else
        {
            JWNLog("获取类空间失败")
            return nil
        }
        let classObject: AnyClass? = NSClassFromString(spaceName + "." + childControllerName)
        guard let vcClassObject = classObject as? UIViewController.Type else
        {
            JWNLog("获取类失败")
            return nil
        }
        let childCon = vcClassObject.init()
        return childCon
    }
    private func addComposeButton() -> Void
    {
        tabBar.addSubview(composeButton)
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count)
        let height = tabBar.bounds.height
        composeButton.frame = CGRectMake(2 * width, 0, width, height)
    }
    // MARK: - layz
    private lazy var composeButton: UIButton = {
        let button = UIButton.init(imageName: "tabbar_compose_icon_add", backGroundImage: "tabbar_compose_button")
        button.addTarget(self, action: Selector("composeButtonClick"), forControlEvents: .TouchUpInside)
        return button
    }()
    // 加@objc支持动态派发
    @objc private func composeButtonClick() -> Void {
        
        selectedIndex = 2
    }
}
